package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class AddressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{
    private ArrayList<Person> addressBook;
    
    public AddressBook()
    {
        addressBook = new ArrayList<Person>();
    }
    
    public void updatePerson(Person person, String name, String address)
    {
        for (Person current : addressBook)
        {
            if (current == person)
            {
                current.setName(name);
                current.setAddress(address);
            }
        }
    }
    
    public void addPerson(Person person)
    {
        // put your code here
        addressBook.add(person);
    }
    
    public void deletePerson(long id)
    {
        for(int i=0; i<addressBook.size(); i++)
        {
            if(addressBook.get(i).getId()== id)
            {
                addressBook.remove(i);
            }
        }
    }
    
    public Person findPerson(long id)
    {
        boolean found = false;
        int index = 0;
        while(index < addressBook.size() && !found)
        {
            Person p = addressBook.get(index);
            if(p.getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }//end while
        
        if(found)
        {
            return addressBook.get(index);
        }
        else
        {
            return null;
        }
    }
    
    public ArrayList getAddressBook()
    {
        return addressBook;
    }
    
    public void listAll()
    {
        for(Person p : addressBook)
        {
            System.out.println("ID : " + p.getId() + "\n" + "Name : " + p.getName()+ "\n");
        }
    }
}
